<?php
declare(strict_types=1);

namespace App\AdminApi\Modular\User\Controller;

use App\AdminApi\BasicsController;
use App\AdminApi\Modular\User\Service\TagsService;
use App\Common\Extend\Guard\Auth;
use Core\HyperfUnit\Response;
use Core\Package\ElementPlus\FormCreate\ElemenuForm;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Psr\Http\Message\ResponseInterface;
use App\AdminApi\Modular\User\Validate\UserTagsValidate;
use Zms\HyperfValidateAnnotations\Validate;

#[
    Auth(guard: self::GUARD, label: ['用户管理', '用户标签'], app: self::APP),
    Controller(prefix: "/admin-api/user-tags", server: self::SERVER)
]
class TagsController extends BasicsController
{
    #[Inject]
    protected TagsService $tags;


    #[
        GetMapping('lists'),
        Auth(sign: 'user.tags.lists', view: '/user/tags/index')
    ]
    public function lists(): ResponseInterface
    {
        return Response::Api()->success(
            data: $this->tags->search()->orderByRaw($this->orderBy('sort DESC,id DESC'))->get()->toArray()
        );
    }

    #[
        RequestMapping('created'),
        Auth(sign: 'user.tags.created', logger: "POST"),
        ElemenuForm(TagsService::class, 'tagsForm'),
        Validate(class: UserTagsValidate::class, scene: 'created')
    ]
    public function created(): ResponseInterface
    {
        $data = $this->request->getParsedBody();
        if ($this->tags->created($data)) {
            return Response::Api()->success('标签添加成功');
        }
        return Response::Api()->fail('标签添加失败', $data);
    }


    #[
        RequestMapping('updated'),
        Auth(sign: 'user.tags.updated', logger: "POST"),
        ElemenuForm(TagsService::class, 'tagsForm', param: ['id']),
        Validate(class: UserTagsValidate::class, scene: 'updated')
    ]
    public function updated(): ResponseInterface
    {
        $data = $this->request->getParsedBody();
        if ($this->tags->updated($data)) {
            return Response::Api()->success('标签添加成功');
        }
        return Response::Api()->fail('标签添加失败', $data);
    }
}